package com.huaxin.dss2.util;

import static com.alibaba.fastjson.util.TypeUtils.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * @author : luqing
 * @date : 2019/1/23 17:38
 */
public class BeanUtil {

    @SuppressWarnings("unchecked")
    public static <T> T castValue(Object obj,Class<T> clazz){
        if(clazz == boolean.class || clazz == Boolean.class){
            return (T) castToBoolean(obj);
        }
        if(clazz == byte.class || clazz == Byte.class){
            return (T) castToByte(obj);
        }
        if(clazz == char.class || clazz == Character.class){
            return (T) castToChar(obj);
        }
        if(clazz == short.class || clazz == Short.class){
            return (T) castToShort(obj);
        }
        if(clazz == int.class || clazz == Integer.class){
            return (T) castToInt(obj);
        }
        if(clazz == long.class || clazz == Long.class){
            return (T) castToLong(obj);
        }
        if(clazz == float.class || clazz == Float.class){
            return (T) castToFloat(obj);
        }
        if(clazz == double.class || clazz == Double.class){
            return (T) castToDouble(obj);
        }
        if(clazz == String.class){
            return (T) castToString(obj);
        }
        if(clazz == BigDecimal.class){
            return (T) castToBigDecimal(obj);
        }
        if(clazz == BigInteger.class){
            return (T) castToBigInteger(obj);
        }
        if(clazz == Date.class){
            return (T) castToDate(obj);
        }
        if(clazz == java.sql.Date.class){
            return (T) castToSqlDate(obj);
        }
        if(clazz == java.sql.Timestamp.class){
            return (T) castToTimestamp(obj);
        }

        return null;
    }


    public static <T> Boolean checkBasicType(Class<T> clazz) {
        return clazz == boolean.class || clazz == Boolean.class ||
            clazz == byte.class  || clazz == Byte.class ||
            clazz == char.class  || clazz == Character.class ||
            clazz == short.class || clazz == Short.class ||
            clazz == int.class   || clazz == Integer.class ||
            clazz == long.class  || clazz == Long.class ||
            clazz == float.class || clazz == Float.class ||
            clazz == double.class || clazz == Double.class ||
            clazz == String.class ||
            clazz == BigDecimal.class || clazz == BigInteger.class ||
            clazz == Date.class || clazz == java.sql.Date.class || clazz == java.sql.Timestamp.class;
    }

}
